package com.lsw.jgyf.ui.fragment;

import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.lsw.jgyf.R;
import com.lsw.jgyf.adapter.HomeFragment2Adapter;
import com.lsw.jgyf.adapter.PopupWindowSortAdapter;
import com.lsw.jgyf.adapter.SelectionSpecificationsAdapter;
import com.lsw.jgyf.adapter.SelectionSpecificationsBrandAdapter;
import com.lsw.jgyf.adapter.SelectionSpecificationsHeYueJiAdapter;
import com.lsw.jgyf.inject.Constants;
import com.lsw.jgyf.model.PhoneModel;
import com.lsw.jgyf.model.PopupWindowSortModel;
import com.lsw.jgyf.model.ScreenModel;
import com.lsw.jgyf.ui.activity.PhoneDetailsActivity;
import com.lsw.jgyf.ui.base.fragment.BaseFragment;
import com.lsw.pullableview.PullToRefreshLayout;
import com.lsw.pullableview.PullableListView;
import com.lsw.util.DensityUtil;
import com.lsw.util.DeviceUtil;
import com.lsw.util.InputTools;
import com.lsw.util.ToastUtil;
import com.lsw.view.MyGridView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by lsw on 2018/7/8.
 */

public class HomeFragment2 extends BaseFragment<HomeFragment2Presenter> implements HomeFragment2Contract.View, View.OnClickListener, PullToRefreshLayout.OnRefreshListener {
    @BindView(R.id.action_bar)
    RelativeLayout action_bar;
    //返回键
    @BindView(R.id.action_bar_left_btn)
    ImageView btnLeft;
    //标题
    @BindView(R.id.action_bar_title)
    TextView tvTitle;
    @BindView(R.id.ll_xiaoliang)
    LinearLayout ll_xiaoliang;
    @BindView(R.id.tv_xiaoliang)
    TextView tv_xiaoliang;
    @BindView(R.id.iv_xiaoliang)
    ImageView iv_xiaoliang;
    @BindView(R.id.ll_price)
    LinearLayout ll_price;
    @BindView(R.id.tv_price)
    TextView tv_price;
    @BindView(R.id.iv_price)
    ImageView iv_price;
    @BindView(R.id.ll_shaixuan)
    LinearLayout ll_shaixuan;
    @BindView(R.id.tv_shaixuan)
    TextView tv_shaixuan;
    @BindView(R.id.pullableListView)
    PullableListView pullableListView;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout pullToRefreshLayout;
    HomeFragment2Adapter homeFragment2Adapter;
    @BindView(R.id.et_seach)
    EditText et_seach;
    @BindView(R.id.ll_seach)
    LinearLayout ll_seach;
    private String name, brand_id, min_price, max_price, order_by = "1";
    //是否合约机is_contract，1是2不是,全部为空
    String is_contract = "";
    private int page = Constants.PAGE_START;

    @Override
    protected int getlayoutId() {
        return R.layout.fragment_home2;
    }

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected void initEventAndData() {
        pullToRefreshLayout.setOnRefreshListener(this);
        pullableListView.setCanPulldown(true);
        tvTitle.setText("机型");
        btnLeft.setVisibility(View.INVISIBLE);
        ll_xiaoliang.setOnClickListener(this);
        ll_price.setOnClickListener(this);
        ll_shaixuan.setOnClickListener(this);
        homeFragment2Adapter = new HomeFragment2Adapter(mContext);
        pullableListView.setAdapter(homeFragment2Adapter);
        mPresenter.getData(page, name, brand_id, min_price, max_price, order_by, is_contract);
        pullableListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext, PhoneDetailsActivity.class);
                intent.putExtra("id", homeFragment2Adapter.getList().get(position).getId() + "");
                startActivity(intent);
            }
        });
        et_seach.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //TODO:你自己的业务逻辑
                    name = v.getText().toString().trim();
                    page = Constants.PAGE_START;
                    mPresenter.getData(page, name, brand_id, min_price, max_price, order_by, is_contract);
                }
                return false;
            }
        });
        ll_seach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputTools.ShowKeyboard(et_seach);
            }
        });
    }

    @Override
    public void setData(List<PhoneModel.DataEntity> list, boolean ispage) {
        pullableListView.setCanPullup(ispage);
        if (page == 1)
            homeFragment2Adapter.setData(list);
        else homeFragment2Adapter.addData(list);
    }

    ScreenModel screenModel;

    @Override
    public void setScreen(ScreenModel model) {
        screenModel = model;
        if (screenModel != null)
            initPopuptWindow(screenModel, ll_shaixuan);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_xiaoliang:
                if (TextUtils.equals("3", order_by)) {
                    order_by = "4";
                    iv_xiaoliang.setRotation(180);
                    iv_price.setRotation(0);
                } else {
                    order_by = "3";
                    iv_xiaoliang.setRotation(0);
                    iv_price.setRotation(0);
                }
                tv_xiaoliang.setTextColor(mContext.getResources().getColor(R.color.color_86B74E));
                iv_xiaoliang.setImageResource(R.mipmap.xia_seclect);
                tv_price.setTextColor(mContext.getResources().getColor(R.color.color_444444));
                iv_price.setImageResource(R.mipmap.xia_seclect_no);
                tv_shaixuan.setTextColor(mContext.getResources().getColor(R.color.color_444444));
                name = et_seach.getText().toString().trim();
                min_price = "";
                max_price = "";
                brand_id = "";
                page = Constants.PAGE_START;
                mPresenter.getData(page, name, brand_id, min_price, max_price, order_by, is_contract);
                break;
            case R.id.ll_price:
                if (TextUtils.equals("1", order_by)) {
                    order_by = "2";
                    iv_xiaoliang.setRotation(0);
                    iv_price.setRotation(180);
                } else {
                    order_by = "1";
                    iv_xiaoliang.setRotation(0);
                    iv_price.setRotation(0);
                }
                tv_xiaoliang.setTextColor(mContext.getResources().getColor(R.color.color_444444));
                iv_xiaoliang.setImageResource(R.mipmap.xia_seclect_no);
                tv_price.setTextColor(mContext.getResources().getColor(R.color.color_86B74E));
                iv_price.setImageResource(R.mipmap.xia_seclect);
                tv_shaixuan.setTextColor(mContext.getResources().getColor(R.color.color_444444));
                name = et_seach.getText().toString().trim();
                min_price = "";
                max_price = "";
                brand_id = "";

                page = Constants.PAGE_START;
                mPresenter.getData(page, name, brand_id, min_price, max_price, order_by, is_contract);
                break;
            case R.id.ll_shaixuan:
                if (screenModel == null)
                    mPresenter.getScreen();
                else initPopuptWindow(screenModel, ll_shaixuan);
                break;
        }
    }

    @Override
    protected void lazyLoadData() {

    }

    @Override
    public void RefreshFail() {
        pullToRefreshLayout.Resultfinish(PullToRefreshLayout.FAIL);
    }

    @Override
    public void RefreshSucceed() {
        pullToRefreshLayout.Resultfinish(PullToRefreshLayout.SUCCEED);
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        name = et_seach.getText().toString().trim();
        page = Constants.PAGE_START;
        mPresenter.getData(page, name, brand_id, min_price, max_price, order_by, is_contract);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        mPresenter.getData(++page, name, brand_id, min_price, max_price, order_by, is_contract);
    }

    PopupWindow popupWindow;
    private List<String> list1 = new ArrayList<>();
    View popupWindow_view;
    MyGridView gridview1, gridview2, gridview3;
    SelectionSpecificationsAdapter selectionSpecificationsAdapter;
    SelectionSpecificationsBrandAdapter selectionSpecificationsBrandAdapter;
    SelectionSpecificationsHeYueJiAdapter selectionSpecificationsHeYueJiAdapter;
    LinearLayout ll_view;
    LinearLayout linearLayout;
    ScrollView scrollView;
    //重置，确定
    TextView tv_chongzhi, tv_true;
    //最小值，最大值
    EditText et_min, et_max;

    /**
     * 创建PopupWindow
     */
    protected void initPopuptWindow(ScreenModel model, View view_bottom) {
        // TODO Auto-generated method stub
        // 获取自定义布局文件activity_popupwindow_left.xml的视图
        popupWindow_view = LayoutInflater.from(mContext).inflate(R.layout.popwindow_screen, null,
                false);
        gridview1 = popupWindow_view.findViewById(R.id.gridview1);
        gridview2 = popupWindow_view.findViewById(R.id.gridview2);
        gridview3 = popupWindow_view.findViewById(R.id.gridview3);
        ll_view = popupWindow_view.findViewById(R.id.ll_view);
        linearLayout = popupWindow_view.findViewById(R.id.linearLayout);
        scrollView = popupWindow_view.findViewById(R.id.scrollView);
        tv_chongzhi = popupWindow_view.findViewById(R.id.tv_chongzhi);
        tv_true = popupWindow_view.findViewById(R.id.tv_true);
        et_min = popupWindow_view.findViewById(R.id.et_min);
        et_max = popupWindow_view.findViewById(R.id.et_max);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(mContext, 50) + DensityUtil.getStatusBarHeight(mContext));
        ll_view.setLayoutParams(params);
        selectionSpecificationsAdapter = new SelectionSpecificationsAdapter(mContext);
        selectionSpecificationsBrandAdapter = new SelectionSpecificationsBrandAdapter(mContext);
        selectionSpecificationsHeYueJiAdapter = new SelectionSpecificationsHeYueJiAdapter(mContext);
        List<String> list = new ArrayList<>();
        list.add("合约机");
        list.add("非合约机");
        gridview3.setAdapter(selectionSpecificationsHeYueJiAdapter);
        selectionSpecificationsHeYueJiAdapter.setData(list);
        gridview1.setAdapter(selectionSpecificationsAdapter);
        gridview2.setAdapter(selectionSpecificationsBrandAdapter);
        selectionSpecificationsAdapter.setData(model.getPrice_range());
        selectionSpecificationsBrandAdapter.setData(model.getBrand_list());
        selectionSpecificationsAdapter.setIsseclect(-1);
        selectionSpecificationsBrandAdapter.setIsseclect(-1);
        selectionSpecificationsHeYueJiAdapter.setIsseclect(-1);
        gridview1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectionSpecificationsAdapter.setIsseclect(position);
                et_min.setText(selectionSpecificationsAdapter.getList().get(selectionSpecificationsAdapter.getIsseclect()).getMin_price());
                et_max.setText(selectionSpecificationsAdapter.getList().get(selectionSpecificationsAdapter.getIsseclect()).getMax_price());
            }
        });
        gridview2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectionSpecificationsBrandAdapter.setIsseclect(position);
            }
        });
        gridview3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectionSpecificationsHeYueJiAdapter.setIsseclect(position);
                if (position == 0) {
                    is_contract = "1";
                } else if (position == 1) {
                    is_contract = "2";
                }
            }
        });
//        }
        popupWindow = new PopupWindow(popupWindow_view, DensityUtil.getWidth(mContext), DensityUtil.getScreenHeight(mContext), true);
//        // 设置动画效果
        popupWindow.setAnimationStyle(R.style.popwindowAnim);
        // 点击其他地方消失
        if (Build.VERSION.SDK_INT < 24) {
            popupWindow.showAsDropDown(view_bottom);
        } else {
            // 适配 android 7.0
            int[] location = new int[2];
            view_bottom.getLocationOnScreen(location);
            int x = location[0];
            int y = location[1];
            popupWindow.showAtLocation(view_bottom, Gravity.CENTER, 0, 0);
        }
        popupWindow.setOnDismissListener(() -> {
            popupWindow = null;
        });
        linearLayout.setOnClickListener(v -> {
            popupWindow.dismiss();
        });
        scrollView.setOnClickListener(v -> {

        });
        tv_chongzhi.setOnClickListener(v -> {
            et_max.setText("");
            et_min.setText("");
            selectionSpecificationsAdapter.setIsseclect(-1);
            selectionSpecificationsBrandAdapter.setIsseclect(-1);
            is_contract = "";
        });
        tv_true.setOnClickListener(v -> {
            popupWindow.dismiss();
            if (selectionSpecificationsBrandAdapter.getIsseclect() < 0) {
                brand_id = "";
            } else
                brand_id = selectionSpecificationsBrandAdapter.getList().get(selectionSpecificationsBrandAdapter.getIsseclect()).getId() + "";
            min_price = et_min.getText().toString().trim();
            max_price = et_max.getText().toString().trim();
            page = Constants.PAGE_START;
            order_by = "";
            name = et_seach.getText().toString().trim();
            tv_xiaoliang.setTextColor(mContext.getResources().getColor(R.color.color_444444));
            iv_xiaoliang.setImageResource(R.mipmap.xia_seclect_no);
            tv_price.setTextColor(mContext.getResources().getColor(R.color.color_444444));
            iv_price.setImageResource(R.mipmap.xia_seclect_no);
            tv_shaixuan.setTextColor(mContext.getResources().getColor(R.color.color_86B74E));
            mPresenter.getData(page, name, brand_id, min_price, max_price, order_by, is_contract);
        });
    }


}
